#include <iostream>
#include <algorithm>

using namespace std;

int ipt[32];
int dp[32][32] = {};
int pi[32][32] = {};
int rst[32];
int n;

void fun(int left, int right)
{
    if (left == right)
    {
        dp[left][right] = ipt[left];
        pi[left][right] = left;
        return;
    }
    if (left > right) 
    {
        dp[left][right] = 1;
        return;
    } 
    if (dp[left][right]) return;
    int mx = 0;
    for (int i = left; i <= right; i++)
    {
        fun(left, i - 1);
        fun(i + 1, right);
        // cout << i << " " << dp[left][i - 1] << " " << dp[i + 1][right] << endl;
        int tp = ipt[i] + dp[left][i - 1] * dp[i + 1][right];
        if (tp > mx)
        {
            mx = tp;
            pi[left][right] = i;
        }
    }
    // cout << left << "," << right << "->" << mx << endl;
    dp[left][right] = mx;
}

void output(int left, int right)
{
    if (left < 1 || right > n)
    {
        return;
    }
    if (left > right)
    {
        return;
    }
    
    cout << pi[left][right] << " ";
    output(left, pi[left][right] - 1);
    output(pi[left][right] + 1, right);
}

int main(int argc, char const *argv[])
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> ipt[i];
    }
    fun(1, n);
    cout << dp[1][n] << endl;
    output(1, n);    
    return 0;
}
